<template>
  <div class="app-container">
        <el-button type="primary" @click="add_group = true" icon="el-icon-plus" size="mini" style="margin-bottom: 10px;">添加一个项目组</el-button>
    <el-table v-loading="listLoading" :data="list" row-key="id" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="ID" width="65">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column width="180px" align="center" label="项目组名称">
        <template slot-scope="{row}">
          <span>{{ row.nickName }}</span>
        </template>
      </el-table-column>

      <el-table-column width="100px" label="组标记">
        <template slot-scope="{row}">
          <span>{{ row.groupName }}</span>
        </template>
      </el-table-column>

      <el-table-column label="主机列表">
        <template slot-scope="{row}">
            <el-tag
            v-for="tag in row.hosts" :key="tag.ip">
            {{tag.hostname}}
          </el-tag>
        </template>
      </el-table-column>

    </el-table>

    <el-dialog
      title="添加主机"
      :visible.sync="add_group"
      width="60%"
      center>

        <el-row class="demo-autocomplete">
          <el-col :span="12">
                <div class="demo-input-suffix" style="padding-bottom: 5px">项目组名称</div>
                <el-input v-model="groupName" style="width:98%"></el-input><br>
          </el-col>
          <el-col :span="12">
              <div class="demo-input-suffix" style="padding-bottom: 5px;">描述</div>
              <el-input v-model="nickName" style="width:98%"></el-input>
          </el-col>
          <el-col>
              <div class="demo-input-suffix" style="padding-bottom: 5px;">主机组</div>

<p style="text-align: center; margin: 0 0 20px">使用 render-content 自定义数据项</p>
  <div style="text-align: center">
    <el-transfer
      style="text-align: left; display: inline-block"
      v-model="host_list"
      filterable
      :left-default-checked="[2, 3]"
      :right-default-checked="[1]"
      :render-content="renderFunc"
      :titles="['未选主机', '已选主机']"
      :button-texts="['移除', '添加']"
      :format="{
        noChecked: '${total}',
        hasChecked: '${checked}/${total}'
      }"
      @change="handleChange"
      :data="data">
      <el-button class="transfer-footer" slot="left-footer" size="small">操作</el-button>
      <el-button class="transfer-footer" slot="right-footer" size="small">操作</el-button>
    </el-transfer>
  </div>



          </el-col>
        </el-row><br>

      <span slot="footer" class="dialog-footer">
        <el-button plain size="small" @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" plain size="small"  @click="submit_playbook">添 加</el-button>
      </span>
    </el-dialog>


  </div>
</template>

<script>
import { fetchList } from '@/api/ansibleui'
import Sortable from 'sortablejs'

export default {
  name: 'DragTable',
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
      const generateData = _ => {
        const data = [];
        for (let i = 1; i <= 15; i++) {
          data.push({
            key: i,
            label: `备选项 ${ i }`,
            disabled: i % 4 === 0
          });
        }
        return data;
      };
    return {
      groupName: "",
      nickName: "",
      host_list: [1],
      add_group: true,
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      },
      sortable: null,
      oldList: [],
      newList: [],
      data: generateData(),
      renderFunc(h, option) {
        return <span>{ option.key } - { option.label }</span>;
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleChange(value, direction, movedKeys) {
        console.log(value, direction, movedKeys);
    },
    submit_playbook(){},
    async getList() {
      this.listLoading = true
      const { data } = await fetchList('groups', this.listQuery)
      this.list = data.items
      this.total = 200
      this.listLoading = false
      this.oldList = this.list.map(v => v.id)
      this.newList = this.oldList.slice()
      // this.$nextTick(() => {
      //   this.setSort()
      // })
    },
    // setSort() {
    //   const el = this.$refs.dragTable.$el.querySelectorAll('.el-table__body-wrapper > table > tbody')[0]
    //   this.sortable = Sortable.create(el, {
    //     ghostClass: 'sortable-ghost', // Class name for the drop placeholder,
    //     setData: function(dataTransfer) {
    //       // to avoid Firefox bug
    //       // Detail see : https://github.com/RubaXa/Sortable/issues/1012
    //       dataTransfer.setData('Text', '')
    //     },
    //     onEnd: evt => {
    //       const targetRow = this.list.splice(evt.oldIndex, 1)[0]
    //       this.list.splice(evt.newIndex, 0, targetRow)

    //       // for show the changes, you can delete in you code
    //       const tempIndex = this.newList.splice(evt.oldIndex, 1)[0]
    //       this.newList.splice(evt.newIndex, 0, tempIndex)
    //     }
    //   })
    // }
  }
}
</script>

<style>
  .transfer-footer {
    margin-left: 20px;
    padding: 6px 5px;
  }
.sortable-ghost{
  opacity: .8;
  color: #fff!important;
  background: #42b983!important;
}
</style>

<style scoped>
.icon-star{
  margin-right:2px;
}
.drag-handler{
  width: 20px;
  height: 20px;
  cursor: pointer;
}
.show-d{
  margin-top: 15px;
}
</style>
